/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 				</p>
 * <p>		    All right reserved. 		   			</p>
 * <p>项目名称 ：深圳公路信息资源整合及国省检日常化监管管理        				</p>
 * <p>创建者 ：hey										</p>
 * <p>描   述  ：路政管理 - 执法监督 - 举报渠道畅通			 			</p>
 * <p>最后修改 ：$: 2017年9月4日-上午10:35:21 v 1.0.0	 hey   $   </p>
 * 
*/

package com.pengtu.gsj.controller.checker.lzgl.overloadMan;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.pengtu.gsj.controller.base.CheckTableBaseController;
import com.pengtu.gsj.entity.app.Sysmenu;
import com.pengtu.gsj.entity.check.CheckProject;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.lzgl.GovernSpeed;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.DownService;
import com.pengtu.gsj.service.check.lzgl.overloadMan.OverloadMechService;
import com.pengtu.gsj.utils.LoadDataUtils;
import com.pengtu.gsj.utils.MathsUtils;
import com.pengtu.gsj.utils.web.SpringMvcUtils;

/**
 * 
 * OverloadMechController
 * 
 * 2017年9月4日 上午10:30:20
 * 
 * @version 1.0.0
 * 
 */
@Controller
@RequestMapping("overloadMech")
public class OverloadMechController extends CheckTableBaseController{
	@Autowired
	private CheckTableService checkTableService;
	@Autowired
	private OverloadMechService overloadMechService;
	@Autowired
	private DownService downService;
	
	/**
	 * 
	 * goToPage:
	 * 适用:跳转页面
	 * @param model
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("show")
	@Token(save = true)
	public String goToPage(Model model) {
		String msg = SpringMvcUtils.getParameter("msg");
		if (StringUtils.isNotEmpty(msg)) {
			model.addAttribute("msg", msg);
		}
		return "check/lzgl/overloadMan/overloadMech";
	}
	
	/**
	 * 
	 * getSearchMap:
	 * 适用:查询，现场检查，默认查询
	 * @param checkTable
	 * @param checkProject
	 * @param sysmenu
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping(value = "search", produces = "application/json; charset=utf-8")
	public Map<String, Object> getSearchMap(@ModelAttribute CheckTable checkTable, @ModelAttribute CheckProject checkProject, @ModelAttribute Sysmenu sysmenu) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		//返回数据和业务文件，以及得分情况
		map = overloadMechService.getDevelopPlanmMap(checkProject.getStartTime(), checkProject.getEndTime(), checkTable, sysmenu == null ? "" : sysmenu.getTag());
		//附件查询
		return map;
		
	}
	
	/**
	 * save:
	 * 适用:保存
	 * @param checkTable
	 * @param checkProject
	 * @param attributes
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("save")
	@Token(remove = true)
	public String save(@ModelAttribute CheckTable checkTable, @ModelAttribute CheckProject checkProject, RedirectAttributes attributes){
		checkTable.setCheckProject(checkProject);
		double tableFull = checkTable.getYearFull() + checkTable.getPoitMark() + checkTable.getPoitMark();//每一个检查站的满分
		checkTable.setTableFull(MathsUtils.formatDouble(tableFull*4.0, 2)); //设置当前检查表格的总分
		checkTableService.save(checkTable);
		attributesParams(checkTable, attributes);
		return "redirect:/overloadMech/show.do";
	}
	
	/**
	 * 
	 * downWordAndZip:
	 * 适用:下载治超制度的表内容
	 * @param checkProject
	 * @param checkTable
	 * @param sysmenu
	 * @return
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@ResponseBody
	@RequestMapping("downWord")
	public ResponseEntity<byte[]> downWordAndZip(@ModelAttribute CheckProject checkProject, @ModelAttribute CheckTable checkTable, @ModelAttribute Sysmenu sysmenu) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		//根据搜索条件查询出来的相关结果
		resultMap = overloadMechService.getDevelopPlanmMap(checkProject.getStartTime(), checkProject.getEndTime(), checkTable, sysmenu == null ? "" : sysmenu.getTag());
		String sysName = sysmenu == null ? "" : sysmenu.getName();
		//将数据记录关联的文件添加到list集合中 在service中一起压缩
		return downService.downWordAndZip(SpringMvcUtils.getParameter("checkSubmoduleName"), SpringMvcUtils.getParameter("checkModuleName"), sysName, "roadAdmin/治超机制.xml", resultMap);
	}
	
	/**
	 * overloadMechData:
	 * 适用:页面进行条件搜索
	 * @param projectId
	 * @return 
	 * @exception 
	 * @since  1.0.0
	*/
	@ResponseBody
	@RequestMapping(value = "overloadMechData")
	public List<Map<String, Object>> overloadMechData(String projectId){
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<GovernSpeed> oList = Lists.newArrayList();
		oList = LoadDataUtils.getGovernSpeed(projectId);
		for (GovernSpeed governSpeed : oList) {
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", governSpeed.getId());
			map.put("pId", 0);
			map.put("name", governSpeed.getStationName());
			mapList.add(map);
		}
		return(mapList);
	}
}
